Kernel.module.define('weatherzone', {

	title: 'weatherzone',
	file: 'weatherzone.php',
	widget: true,

	init: function() {
		var module = this;
		var lastRefresh = new Date(localStorage.getItem("CONFIG_WEATHER_LAST_UPDATE"));
		var lr = new Date(lastRefresh.getTime() + 3 * 60 * 60 * 1000);
		var nowDT = new Date();

		module.getRSS('widgets/weatherzone/ajaxGetRSS.php');
		$("#waj").html("<h3>weather<i class='fa fa-refresh'></i></h3>");
		$.get('widgets/weatherzone/ajaxGetRSS.php?currenttemp=1', function (data2) {
			var tempNow = data2;

			var item = $(data2).find('forecast').find("area[aac='VIC_PT042']");
			$("#current_temp").html(tempNow + "&deg; Now");
			$("#dashWeatherAjax").html("<h3>Weather</h3>");
		});

		if (nowDT > lr) {
		$("#waj").html("<h3>weather<i class='fa fa-refresh'></i></h3>");
			module.getRSS('widgets/weatherzone/ajaxGetRSS.php?refresh=1');
			$.get('widgets/weatherzone/ajaxGetRSS.php?currenttemp=1', function (data2) {
				var tempNow = data2;

				var item = $(data2).find('forecast').find("area[aac='VIC_PT042']");
				$("#current_temp").html(tempNow + "&deg; Now");

				localStorage.setItem("CONFIG_WEATHER_LAST_UPDATE", nowDT);
				$("#dashWeatherAjax").html("<h3>Weather</h3>");
			});
		}
	},
	
	getRSS: function(url) {
		var module = this;
		
		$.get(url, function (data) {
			var dayName = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
			var today = new Date();
			var day2Name = today.getDay();
			
			var item = $(data).find('forecast').find("area[aac='VIC_PT042']");
			var wicon = item.find("forecast-period[index='0']").find("element[type='forecast_icon_code']").text();
			var mintemp = item.find("forecast-period[index='0']").find("element[type='air_temperature_minimum']").text();
			var maxtemp = item.find("forecast-period[index='0']").find("element[type='air_temperature_maximum']").text();
			var wdesc = item.find("forecast-period[index='0']").find("text[type='precis']").text();
			var rainprob = item.find("forecast-period[index='0']").find("text[type='probability_of_precipitation']").text();

			switch (wicon) {
				case '1':
					wicon_img = "<img src='widgets/weatherzone/images/icon/sunny.png'>";
					break;
				case '2':
					wicon_img = "<img src='widgets/weatherzone/images/icon/clear.png'>";
					break;
				case '3':
					wicon_img = "<img src='widgets/weatherzone/images/icon/partly-cloudy.png' width='75' valign='center'>";
					break;
				case '8':
					wicon_img = "<img src='widgets/weatherzone/images/icon/light-rain.png' width='75' valign='center'>";
					break;
				case '9':
					wicon_img = "<img src='widgets/weatherzone/images/icon/wind.png'>";
					break;
				case '11':
					wicon_img = "<img src='widgets/weatherzone/images/icon/showers.png'>";
					break;
				case '12':
					wicon_img = "<img src='widgets/weatherzone/images/icon/rain.png'>";
					break;
				case '16':
					wicon_img = "<img src='widgets/weatherzone/images/icon/storm.png'>";
					break;
				case '17':
					wicon_img = "<img src='widgets/weatherzone/images/icon/light-showers.png'>";
					break;
			}

			if (wicon_img !== "") {
				$("#weather_icon").html("<div style='float: left; width: 80px; padding: 20px;'>" + wicon_img + "</div>");
			}
			$("#weatherinfo").html("<div style='float: left; width: 270px; padding: 10px;>");
			$("#weatherinfo").append(" " + wdesc + "<br>");
			if (mintemp.trim() !== "") $("#weatherinfo").append("<b>Low</b>: " + mintemp + "&deg; / ");
			if (maxtemp.trim() !== "") $("#weatherinfo").append("<b>Max</b>: " + maxtemp +  "&deg; <br>");
			$("#weatherinfo").append("<b>Rain</b>: " + rainprob);
			$("#weatherinfo").append("</div><br><br>");
			
			$("#weatherforecast").html("");
			for (i = 1; i < 6; i++) {
				if (day2Name + 1 > 6) day2Name = 0; else day2Name += 1;
				var tempIcon = item.find("forecast-period[index='"+i+"']").find("element[type='forecast_icon_code']").text();
				var minTemp = item.find("forecast-period[index='"+i+"']").find("element[type='air_temperature_minimum']").text();
				var maxTemp = item.find("forecast-period[index='"+i+"']").find("element[type='air_temperature_maximum']").text();
				var tempDesc = item.find("forecast-period[index='"+i+"']").find("text[type='precis']").text();

				$("#weatherforecast").append("<div style='float: left;'><b>"+dayName[day2Name] + "</b> " + tempDesc + "</div> <div style='float; left; text-align: right;'><span class='lowTemp'>" + minTemp + "&deg;</span> <span class='highTemp'>" + maxTemp + "&deg;</span></div>");
			}
		});
	}
})

